package ru.jexp.rcp.supply.actions;

import org.eclipse.jface.action.Action;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;

import ru.jexp.rcp.supply.img.JExpImageLink;
import ru.jexp.rcp.supply.mui.JExpSupplyMessages;
import ru.jexp.rcp.supply.view.ViewPartNavigation;

public class ActionOpenNavigationView extends Action implements IPartListener {

	public static final String ID = "ru.jexp.rcp.supply.actions.actionopennavigationview";
	private final IWorkbenchWindow window;

	public ActionOpenNavigationView(IWorkbenchWindow window) {
		this.window = window;
		setId(ID);
		setActionDefinitionId(ID);
		setText(JExpSupplyMessages.Open_nav_view_pattern_action_text);
		setToolTipText(JExpSupplyMessages.Open_nav_view_pattern_action_tooltip);
		setImageDescriptor(JExpImageLink
				.getImageDescriptor(JExpImageLink.VIEW_NAV));
		window.getPartService().addPartListener(this);
	}

	@Override
	public void run() {
		try {
			window.getActivePage().showView(ViewPartNavigation.ID);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void partActivated(IWorkbenchPart part) {
	}

	@Override
	public void partBroughtToTop(IWorkbenchPart part) {
	}

	@Override
	public void partClosed(IWorkbenchPart part) {
		if (part instanceof ViewPartNavigation) {
			setEnabled(true);
		}
	}

	@Override
	public void partDeactivated(IWorkbenchPart part) {
	}

	@Override
	public void partOpened(IWorkbenchPart part) {
		if (part instanceof ViewPartNavigation) {
			setEnabled(false);
		}
	}
}
